﻿using CL.Business.BLL;
using System;
using System.Web.UI;
using CL.Business.Entity;
using CL.Enum.Common;

namespace CL.Admin.admin.promotion
{
    public partial class Commission_rebate_edit : UI.AdminPage
    {
        private string action = CaileEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = Request.QueryString["action"];
            //如果是编辑则检查信息是否存在
            if (_action == CaileEnums.ActionEnum.Edit.ToString())
            {
                this.action = _action;//修改类型
                int.TryParse(Request.QueryString["id"], out this.id);
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new RebateBLL().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }


            if (!Page.IsPostBack)
            {
                ChkAdminLevel("Commission_rebate", CaileEnums.ActionEnum.View.ToString()); //检查权限
                if (action == CaileEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }

        }

        #region 增加操作=================================
        private bool DoAdd()
        {
            RebateBLL bll = new RebateBLL();
            RebateEntity model = new RebateEntity();
            string AccountName = AgencyAcccount.Value;
            string Account = HidAgencyID.Value.Trim();
            string RebateNum = txtRebateNum.Text;
            string SaleAmount = txtAmount.Text;
            string SettlementNum=  txtSettlementNum.Text;
            string TaxPoint= txtTaxPoint.Text;
            if (bll.AccountExists(int.Parse(Account),long.Parse(SaleAmount)))
            {
                JscriptMsg("该代理销售金额返点等级设置已存在，不能重复添加！", "Commission_rebate.aspx");
                return false;
            }
            model.AccountID = int.Parse(Account);
            model.RebateNum =int.Parse(RebateNum);
            model.Amount = int.Parse(SaleAmount);
            model.CreateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            model.EfficaciousTime  = Convert.ToDateTime(txtEffectTime.Text);
            model.ModifyTime = DateTime.Now.AddMonths(1);
            model.SettlementNum =long.Parse(SettlementNum);
            model.TaxPoint = int.Parse(TaxPoint);
            bool result = false;
            if (bll.InsertEntity(model) > 0)
            {
                new RecordBLL().InsertEntity(new Business.Entity.RecordEntity()
                {
                    AccountID = int.Parse(Account),
                    RecordTime = DateTime.Now,
                    RecordRemark = string.Format("创建返点"),
                    RecordType = 3
                });
                AddAdminLog(CaileEnums.ActionEnum.Add.ToString(), "添加代理返点设置:" + AccountName); //记录日志
                result = true;
            }
            return result;
        }
        #endregion



        #region 赋值操作
        private void ShowInfo(int _id)
        {
            RebateBLL bll = new RebateBLL();
            RebateEntity model = bll.QueryEntity(_id);
            AgencyAcccount.Value=  new AccountBLL().QueryEntity(model.AccountID).AccountCode;
            HidAgencyID.Value = model.AccountID.ToString();
            txtRebateNum.Text = model.RebateNum.ToString();
            txtAmount.Text = model.Amount.ToString();
            txtEffectTime.Text = model.EfficaciousTime.ToString("yyyy-MM-dd");
            txtSettlementNum.Text = model.SettlementNum.ToString();
            txtTaxPoint.Text = model.TaxPoint.ToString();
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            RebateBLL bll = new RebateBLL();
            RebateEntity model = bll.QueryEntity(_id);
            string AccountName =  AgencyAcccount.Value;
            string Account = HidAgencyID.Value.Trim();
            string RebateNum = txtRebateNum.Text;
            string SaleAmount = txtAmount.Text;
            string TaxPoint = txtTaxPoint.Text;
            string SettlementNum = txtSettlementNum.Text;
            if (model.AccountID!=int.Parse(Account)&& bll.AccountExists(int.Parse(Account),long.Parse(SaleAmount)))
            {
                JscriptMsg("该代理返点设置已存在，不能重复添加！", "Commission_rebate.aspx");
                return false;
            }
            RebateEntity entity = new RebateEntity();
            entity.RebateID = _id;
            entity.Amount = long.Parse(SaleAmount);
            entity.CreateTime = Convert.ToDateTime(model.CreateTime);
            entity.EfficaciousTime =Convert.ToDateTime(txtEffectTime.Text);
            entity.RebateNum = int.Parse(RebateNum);
            entity.SettlementNum = long.Parse(SettlementNum);
            entity.TaxPoint = int.Parse(TaxPoint==""?"0": TaxPoint);
            entity.AccountID = int.Parse(Account);
            entity.ModifyTime =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            if (bll.ModifyEntity(entity) > 0)
            {
                new RecordBLL().InsertEntity(new Business.Entity.RecordEntity()
                {
                    AccountID = int.Parse(Account),
                    RecordTime = DateTime.Now,
                    RecordRemark = string.Format("修改返点"),
                    RecordType = 4

                });
                AddAdminLog(CaileEnums.ActionEnum.Edit.ToString(), "修改代理人返点设置:" + AccountName); //记录日志
                result = true;
            }
            return result;
        }
        #endregion


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == CaileEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("Commission_rebate", CaileEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦！", string.Empty);
                    return;
                }
                JscriptMsg("修改返点设置成功！", "Commission_rebate.aspx");
            }
            else
            {
                ChkAdminLevel("Commission_rebate", CaileEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误啦！", string.Empty);
                    return;
                }
                JscriptMsg("添加成功！", "Commission_rebate.aspx");
            }
        }
    }
}